// networkHandler.h
// Written by Andi Cescolini, Sam Tarin, and Kevin Sonnen

#ifndef	NETWORKHANDLER_H
#define NETWORKHANDLER_H

#include <string>
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
//#include "../situationControl/SituationController.h"
#include "situation_wrapper.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
//#include <boost/thread/thread.hpp>

// Code snippets taken from example code:
// chat_client.cpp

#include <cstdlib>
#include <deque>
#include <boost/bind.hpp>

using boost::asio::ip::tcp;

typedef std::deque<sit_wrap::situation_wrapper> game_situation_queue;

namespace networkI
{
class networkHandler {

	public:
		networkHandler();
		//networkHandler(SituationController*);
		~networkHandler();
		int disconnect();
		int sendSituation(Situation sit);
		Situation recvSituation(sit_wrap::situation_wrapper);
		void write(const sit_wrap::situation_wrapper& msg);
		void close();
		Situation MySituation;	// Added for testing purposes
		boost::asio::io_service* getIOservice(){ return &Myio_service; };
		bool getNeedToReconcile() { return needToReconcile; };
		void setNeedToReconcile(bool value) { needToReconcile = value; };
	private:
		boost::asio::io_service Myio_service;
		boost::asio::ip::tcp::socket * Mysocket;
		boost::asio::ip::tcp::resolver * Myresolver;
    		boost::asio::ip::tcp::resolver::query * Myquery;
    		boost::asio::ip::tcp::resolver::iterator Myiterator;
	  	sit_wrap::situation_wrapper read_msg_;
  		game_situation_queue write_msgs_;
		//boost::thread * Mythread;
		void handle_connect(const boost::system::error_code& error);
		void handle_read_header(const boost::system::error_code& error);
		void handle_read_body(const boost::system::error_code& error);
		void do_write(sit_wrap::situation_wrapper msg);
		void handle_write(const boost::system::error_code& error);
		void do_close();
		bool needToReconcile;
		//SituationController* SitControl;

};
} // End networkI namespace

#endif
